#include <stdio.h>
#include <stdint.h>
#include <avr/interrupt.h>


static uint32_t m_clock;


ISR(TIMER1_COMPA_vect) {
    m_clock++;
    TCNT1 = 0;  /* Reset timer */
}


void clock_init() {
    TIMSK1 &= ~OCIE1A;  /* Turn off compare interrupt */
    m_clock = 0;        /* Zero clock */
    OCR1A = 2000;       /* Divide by 2000 */
    TCCR1B = 1<<CS11;   /* Prescale by 8 */
    TIMSK1 |= 1<<OCIE1A;/* Turn on compare interrupt */

    TCNT1 = 0;          /* Reset timer */
}


uint32_t clock_time() {
    return m_clock;
}


int clock_cmd(char* line) {
	printf("Clock: %dms from startup.", (int)clock_time());
    
	return 0;
}

